import axios from "axios";
import { ElLoading, ElMessage } from 'element-plus';

let loadingInstance = null;

const request = axios.create({
  baseURL: '/api',
  timeout: 30000
});

request.interceptors.request.use(config => {
  loadingInstance = ElLoading.service({
    lock: true,
    text: 'loading...'
  });
  return config;
});

request.interceptors.response.use(response => {
  loadingInstance.close();
  return response;
}, error => {
  console.log('请求错误==>', error);
  const msg = error.Message !== undefined ? error.Message : '';
  ElMessage({
    showClose: true,
    message: '网络请求错误' + msg,
    type: 'error',
    duration: 3 * 1000
  });
  loadingInstance.close();
  return Promise.reject(error);
});

export default request;